package view.user;

import java.awt.*;
import java.awt.event.*;


import javax.swing.*;
import javax.swing.border.EmptyBorder;


import entity.User;
import service.user.UserService;
import service.user.UserServiceImpl;
import utils.ComonUtil;
import view.MainView;
import view.CommonView.CommonJPanel;
import view.model.UserTypeComboBoxModel;

/**
 * @Author: Lin Hongwei
 * @LastEditors: Lin Hongwei
 * @email: 1365755396@qq.com
 * @CSDN: https://blog.csdn.net/weixin_45429409
 * @Description: 添加用户模块
 *
 */
public class AddUserView extends CommonJPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	// service层
	UserService userService = new UserServiceImpl();

	// 创建输入框
	private JTextField userIdField;
	private JTextField userNameField;
	private JTextField userPassField;
	UserTypeComboBoxModel bcm = new UserTypeComboBoxModel();
	private JComboBox<Object> userTypeComboBox;
	private JComboBox<String> roleComboBox;

	// 创建按钮
	private JButton add, reset;

	private String[] roleArr = { "读者", "管理员" };

	public AddUserView() {
		super(TYPE_TRANSPARENT);
		setLayout(new FlowLayout(FlowLayout.CENTER, 20, 10));
		setBorder(new EmptyBorder(20, 100, 10, 100));

		// 标题面板
		JPanel titlePanel = new CommonJPanel(TYPE_TRANSPARENT);
		add(titlePanel);
		// 内容标题
		JLabel labelAddBook = new JLabel("添加用户");
		labelAddBook.setIcon(new ImageIcon(AddUserView.class.getResource("/img/ADD.png")));
		labelAddBook.setFont(new Font("微软雅黑", Font.BOLD, 20));
		titlePanel.setPreferredSize(new Dimension(MainView.instance.getWidth(), 70));
		titlePanel.add(labelAddBook);

		// 标签、输入框面板
		JPanel mainPanel = new CommonJPanel(TYPE_TRANSPARENT);
		mainPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 30, 20));
		mainPanel.setPreferredSize(new Dimension(MainView.instance.getWidth(), 280));
		add(mainPanel);

		// 创建用户编号标签
		JLabel labelBookId = new JLabel("用户  ID：");
		labelBookId.setPreferredSize(new Dimension(88, 30));
		mainPanel.add(labelBookId);
		// 创建用户编号输入框
		userIdField = new JTextField();
		userIdField.setPreferredSize(new Dimension(300, 30));
		//		userIdField.setEditable(false);
		mainPanel.add(userIdField);

		// 创建用户名称标签
		JLabel labelBookName = new JLabel("用户昵称：");
		labelBookName.setPreferredSize(new Dimension(88, 30));
		mainPanel.add(labelBookName);
		// 创建用户编号输入框
		userNameField = new JTextField();
		userNameField.setPreferredSize(new Dimension(300, 30));
		mainPanel.add(userNameField);

		// 创建密码
		JLabel labelPass = new JLabel("用户密码：");
		labelPass.setPreferredSize(new Dimension(88, 30));
		mainPanel.add(labelPass);
		// 创建密码
		userPassField = new JTextField();
		userPassField.setPreferredSize(new Dimension(300, 30));
		mainPanel.add(userPassField);

		// 创建用户角色
		JLabel labelUserRole = new JLabel("用户角色：");
		labelUserRole.setPreferredSize(new Dimension(88, 30));
		mainPanel.add(labelUserRole);
		// 创建用户下拉框
		roleComboBox = new JComboBox<String>(roleArr);
		roleComboBox.setPreferredSize(new Dimension(300, 30));
		roleComboBox.setBackground(Color.white);
		mainPanel.add(roleComboBox);

		// 身份
		JLabel labelUserType = new JLabel("用户身份：");
		labelUserType.setPreferredSize(new Dimension(88, 30));
		mainPanel.add(labelUserType);
		userTypeComboBox = new JComboBox<>(bcm);
		userTypeComboBox.setPreferredSize(new Dimension(MainView.instance.getWidth() - 150, 30));
		userTypeComboBox.setBackground(Color.white);
		mainPanel.add(userTypeComboBox);

		// 按钮面板
		JPanel buttonPanel = new CommonJPanel(TYPE_TRANSPARENT);
		buttonPanel.setLayout(new GridLayout(1, 2, 80, 0));
		add(buttonPanel);

		// 创建添加按钮
		add = new JButton("添加");
		buttonPanel.add(add);

		// 创建重置按钮
		reset = new JButton("重置");
		buttonPanel.add(reset);

		// 添加按钮事件
		addListener();

		// 重置按钮事件
		reset.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				resetInput();
			}
		});

	}

	/**
	 * 添加按钮事件
	 */
	public void addListener() {
		add.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println(roleComboBox.getSelectedIndex());
				if (ComonUtil.isEmpty(userIdField.getText()) || ComonUtil.isEmpty(userNameField.getText())
						|| ComonUtil.isEmpty(userPassField.getText()) || roleComboBox.getSelectedItem() == null
						|| userTypeComboBox.getSelectedItem() == null) {
					JOptionPane.showMessageDialog(AddUserView.this, "选项不能为空！");
					return;
				}
				User user = new User();
				user.setId(userIdField.getText());
				user.setPassword(userPassField.getText());
				user.setUserName(userNameField.getText());
				user.setAdmin(roleComboBox.getSelectedIndex());
				user.setType(userTypeComboBox.getSelectedItem().toString());

				String flag = userService.addUser(user)?"添加成功！":"添加失败";
				JOptionPane.showMessageDialog(AddUserView.this, flag);
      
			}
		});
	}

	/**
	 * 重置输入框内容
	 */
	public void resetInput() {
		userNameField.setText("");
		userIdField.setText("");
		userPassField.setText("");
		roleComboBox.setSelectedIndex(0);
		userTypeComboBox.setSelectedIndex(-1);
		userTypeComboBox.updateUI();
	}

	/**
	 * 更新数据
	 */
	public void updateData() {
		resetInput();

	}

}
